// pages/myTicket/myTicket.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navId:0,
    ticketList:[],
    noData:true,
    isEnd: false,
    p: 1
  },
  reqData(){
    const that = this;
    let token = app.globalData.token;
    //根据id领取优惠券
    let json = {
      token,
      type: that.data.navId,
      p: 1
    }
    let jsonData = app.utils.getSignData(json);
    that.setData({
      hiddenLoading: false
    })
    app.utils.post(app.api.reqUrls.getCoupon,jsonData).then(res => {
      that.setData({
        hiddenLoading: true
      })
      if (res.status == 1) {
        console.log(app.globalData.address)
        // app.globalData.address = null
       
        res.result.forEach((item, i) => {
          item.use_start_time = app.moment(item.use_start_time * 1000).format('YYYY.MM.DD');
          item.use_end_time = app.moment(item.use_end_time * 1000).format('YYYY.MM.DD');
          item.money = item.money.split('.');
          item.condition = item.condition.split('.');
          if (item.is_plat == 1) {
            item.valid = item.store_ids == '' ? false : item.store_ids.includes(app.globalData.address.select_store_id.toString());
          }else{
            item.valid = item.store_id.toString() == app.globalData.address.select_store_id.toString()
          }
        })
        that.setData({
          ticketList:res.result
        })
        if (that.data.ticketList == '') {
          that.setData({
            noData: false
          })
        } else {
          that.setData({
            noData: true
          })
        }
      } else {
        throw res.msg;
      }
    }).catch(err => {
      that.setData({
        hiddenLoading: true
      })
      app.showInfo(err.toString())
    })
  },


  reqMoreData() {
    const that = this;
    if(that.data.isEnd){
      return
    }
    let token = app.globalData.token;
    //根据id领取优惠券
    let json = {
      token,
      type: that.data.navId,
      p: that.data.p + 1
    }
    let jsonData = app.utils.getSignData(json);
    that.setData({
      hiddenLoading: false
    })
    app.utils.post(app.api.reqUrls.getCoupon, jsonData).then(res => {
      that.setData({
        hiddenLoading: true
      })
      if (res.status == 1) {
        if (res.result != '') {
          res.result.forEach((item, i) => {
            item.use_start_time = app.moment(item.use_start_time * 1000).format('YYYY.MM.DD');
            item.use_end_time = app.moment(item.use_end_time * 1000).format('YYYY.MM.DD');
            item.money = item.money.split('.');
            item.condition = item.condition.split('.');
            if (item.is_plat == 1) {
              item.valid = item.store_ids == '' ? false : item.store_ids.includes(app.globalData.address.select_store_id.toString());
            } else {
              item.valid = item.store_id.toString() == app.globalData.address.select_store_id.toString()
            }
          })
          that.setData({
            ticketList: [...that.data.ticketList, ...res.result],
            p: that.data.p + 1
          })
        }else{
          wx.showToast({
            title: '没有更多了',
            icon: 'none',
            duration: 1000
          })
          that.setData({
            isEnd: true
          })
        }
      } else {
        throw res.msg;
      }
    }).catch(err => {
      that.setData({
        hiddenLoading: true
      })
      app.showInfo(err.toString())
    })
  },

  // 点击nav切换
  navClick(e){
    const that = this;
    if (e.currentTarget.dataset.navid == that.data.navId){
      that.setData({
        isEnd: false,
        p: 1
      })
    }
    that.setData({
      navId: e.currentTarget.dataset.navid
    })
    that.reqData();
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    console.log(app.globalData.address)
    // if (app.globalData.address == null) {
    //   wx.redirectTo({
    //     url: '/page/tabBar/location/location',
    //   })
    // }else{
      that.reqData();
    // }
  },
  onShow: function () {
    //数据统计
    app.statistics("page/detail/pages/myTicket/myTicket", "优惠券")
  },
  onReachBottom(){
    const that = this;
    that.reqMoreData();
  }
})